﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule
{
    public class DictionaryItemsManager : BusinessLogic
    {
        public DictionaryItems Load(decimal ID)
        {
            return (DictionaryItems)ObjectRow.Load(typeof(DictionaryItems), string.Format("this.DictionaryItemID='{0}'", ID));
        }
        
        public Dictionary LoadDict(decimal ID)
        {
            return (Dictionary)ObjectRow.Load(typeof(Dictionary), string.Format("this.DictionaryID='{0}'", ID));
        }

        public ObjectTable GetCourseTable(decimal DictionaryID, decimal domainid)
        {
            return new ObjectTable(typeof(DictionaryItems), string.Format("this.DictionaryID='{0}' and this.DOMAINID='{1}'", DictionaryID, domainid));
        }

        public ObjectTable GetCourseTable(decimal domainid)
        {
            return new ObjectTable(typeof(Dictionary), string.Format("this.DOMAINID='{0}'", domainid));
        }

        public ObjectTable GetDictionaryList(string domainid, string updatetime)
        {
            return new ObjectTable(typeof(DICTIONARYVIEW), string.Format("this.DOMAINID='{0}' and a.LAST_UPDATE_TIME > '{1}'", domainid, updatetime));
        }

        //Create by :Steven Jiang
        //Create Date :2008-12-9 19:04
        /// <summary>
        /// 获取制定类别的字典条目
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="domainid"></param>
        /// <returns></returns>
        public ObjectTable GetItemList(string Code, decimal domainid)
        {
            return new ObjectTable(typeof(DictionaryItems), string.Format("this.DictionaryID in (select DictionaryID from Dictionary where code='{0}' and domain_id='{1}' ) and this.DOMAINID='{2}' and Active=1 ", Code, domainid, domainid));
        }

        public DictionaryItems Load(string DictionaryCode, decimal domainid, string DictionaryItemCode)
        {
            return (DictionaryItems)ObjectRow.Load(typeof(DictionaryItems), string.Format("this.DictionaryID in (select DictionaryID from Dictionary where code='{0}' and domain_id='{1}') AND CODE='{2}'", DictionaryCode,domainid,DictionaryItemCode));
        }
        public ObjectTable GetDictionaryType(decimal domainid)
        {
            return new ObjectTable(typeof(Dictionary), string.Format("this.DOMAINID='{0}'", domainid));
        }

        
        public static DataSet GetDictionaryItems(string DictionaryID)
        {
            return SqlData.OpenSP("usp_SelectPromotionsDictionaryItems", DictionaryID);
        }

        public static DataSet GetDictionaryItemstest(decimal DOMIANID)
        {
            return SqlData.OpenSP("sp_QueryCustomerProductCPR", DOMIANID);
        }

        public decimal Save(DictionaryItems reason)
        {
            if (reason != null)
            {
                try
                {
                    //if (reason.State == DataRowState.Added)
                    //{
                    //    reason.DictionaryItemID = GetCurrentValue("DictionaryItems", 0);
                    //}

                    reason.Save();
                }
                catch (Exception err)
                {
                    message = err.Message;
                    throw err;
                }
                return reason.DictionaryItemID;
            }

            return 0;
        }

        public decimal SaveDict(Dictionary dict)
        {
            if (dict != null)
            {
                try
                {
                   
                    dict.Save();
                }
                catch (Exception err)
                {
                    message = err.Message;
                    throw err;
                }
                return dict.DictionaryID;
            }

            return 0;
        }

        public DataSet GetDictionaryItemsByDictCode(string dictCode)
        {
            string sql = @"SELECT DictionaryItems.* FROM DictionaryItems 
                            INNER JOIN Dictionary ON Dictionary.DictionaryID = DictionaryItems.DictionaryID
                            AND Dictionary.CODE='" + dictCode + "'";
            return SqlData.OpenSql(sql);

        }


        public DataSet GetDictionaryItemByDictCodeAndItemCode(string dictCode,string itemCode,decimal domainId)
        {
            string sql = @"SELECT DictionaryItems.* FROM DictionaryItems 
                            INNER JOIN Dictionary ON Dictionary.DictionaryID = DictionaryItems.DictionaryID
                            AND Dictionary.CODE='" + dictCode + "' AND DictionaryItems.CODE='" + itemCode + "' AND Dictionary.DOMAIN_ID='"+domainId+"'";
            return SqlData.OpenSql(sql);

        }

    }

    ///Create by:Steven Jiang
    ///Create Time: 2008-12-9 19:32
    /// <summary>
    /// 字典枚举
    /// </summary>
    public class DictionaryEnum
    {
        /// <summary>
        /// 销售代表类型
        /// </summary>
        public const string Represent = "DSR_Type";

        /// <summary>
        /// 客户类型
        /// </summary>
        public const string CustomerClass = "Customer_Type";


        /// <summary>
        /// 用户类型
        /// </summary>
        public const string UserClass = "User_Type";

        /// <summary>
        /// 客户渠道
        /// </summary>
        public const string CustomerChannel = "Channel";

        /// <summary>
        /// 区域层次
        /// </summary>
        public const string ChannelType = "KHQD";

        /// <summary>
        /// 销量等级
        /// </summary>
        public const string Store_Level = "Store_Sales_Volumn";

        /// <summary>
        /// 支付类型
        /// </summary>
        public const string PaymentType = "Payment";

        /// <summary>
        /// 促销失败原因
        /// </summary>
        public const string PromotionList = "Failure_Reason";

        /// <summary>
        /// 照片类型
        /// </summary>
        public const string PhotoType = "Photo_Type";

        /// <summary>
        /// 职位类型
        /// </summary>
        public const string UserPosition = "POSITION";

        /// <summary>
        /// 分类等级
        /// </summary>
        public const string OrgLevel = "ORG_LEVEL";

        /// <summary>
        /// 消息类型
        /// </summary>
        public const string MessageType = "Message_Type";

        /// <summary>
        ///资产类型
        /// </summary>
        public const string AssetType = "Asset_Type";

        /// <summary>
        ///人员资产类型
        /// </summary>
        public const string PeopleAsset_Type = "PeopleAsset_Type";

        /// <summary>
        ///资产状态
        /// </summary>
        public const string AssetStatus = "Asset_Status";

        /// <summary>
        /// 资产存放位置
        /// </summary>
        public const string AssetLocation = "Asset_Location";

        /// <summary>
        /// 资产使用状态
        /// </summary>
        public const string AssetPresence = "Asset_Presence";

        /// <summary>
        /// 资产纯净度
        /// </summary>
        public const string AssetPurity = "Asset_Purity";

        /// <summary>
        /// 资产投放方式
        /// </summary>
        public const string AssetLaunch = "Asset_Launch";

        /// <summary>
        /// 承担费用主题
        /// </summary>
        public const string ChargeFrom = "Charge_From";

        /// <summary>
        ///订单来源
        /// </summary>
        public const string OrderForm = "Order_From";

        /// <summary>
        ///订单类型
        /// </summary>
        public const string OrderType = "Order_Type";

        /// <summary>
        ///模板类型
        /// </summary>
        public const string TempleteType = "Templete_Type";

        /// <summary>
        ///重要程度
        /// </summary>
        public const string Important = "Important";

        /// <summary>
        ///教育程度
        /// </summary>
        public const string Education = "Education";

        /// <summary>
        /// 目标输入类型
        /// </summary>
        public const string ObjectiveInputType = "ObjectiveInput_Type";

        /// <summary>
        /// 目标类型
        /// </summary>
        public const string ObjectiveType = "Objective_Type";

        /// <summary>
        /// 输入类型，（KPI/CPR）
        /// </summary>
        public const string KPRCPR_InputTYpe = "KPI/CPR_InputType";
        
    }
}
